package com.archetypus.projectschedule.core.activities.sf;

import javax.transaction.UserTransaction;

import com.archetypus.projectschedule.common.ServiceLocator;
import com.archetypus.projectschedule.common.ServiceLocatorException;
import com.archetypus.projectschedule.common.data.DAOFactory;
import com.archetypus.projectschedule.common.data.activities.ActivityDAO;
import com.archetypus.projectschedule.common.dto.activities.Activity;
import com.archetypus.projectschedule.common.dto.activities.ActivityListHandler;
import com.archetypus.projectschedule.common.dto.projects.Project;
import com.archetypus.projectschedule.common.exceptions.ProjectSchedulerException;

public class ActivitySF implements IActivitySF {
	
	private static ActivitySF instance;
	
	protected ActivitySF(){}
	
	public static ActivitySF getInstance(){
		if(instance == null)
			instance = new ActivitySF();
		
		return instance;
	}
	
	@Override
	public void createActivity(Activity activity) throws ProjectSchedulerException {
		UserTransaction userTransaction = null;
		
		try {
			userTransaction = ServiceLocator.getInstance().lookupUserTransaction();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
			throw new ProjectSchedulerException("Could not create transaction object",e);
		}
		
		try {
			userTransaction.begin();
			DAOFactory daoFactory = DAOFactory.getDAOFactory();
			ActivityDAO activityDAO = daoFactory.getActivityDAO();
			activityDAO.insert(activity);
			userTransaction.commit();
		} catch (Exception e) {
			try{
				userTransaction.rollback();
			}catch(Exception ex){
				ex.printStackTrace();
				throw new ProjectSchedulerException("Could not rollback transaction",ex);
			}
			e.printStackTrace();
			throw new ProjectSchedulerException(e.getMessage(),e);
		}
		
	}

	@Override
	public void deleteActivity(Activity activity) throws ProjectSchedulerException {
		UserTransaction userTransaction = null;
		
		try {
			userTransaction = ServiceLocator.getInstance().lookupUserTransaction();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
			throw new ProjectSchedulerException("Could not create transaction object",e);
		}
		
		try {
			userTransaction.begin();
			DAOFactory daoFactory = DAOFactory.getDAOFactory();
			ActivityDAO activityDAO = daoFactory.getActivityDAO();
			activityDAO.delete(activity);
			userTransaction.commit();
		} catch (Exception e) {
			try{
				userTransaction.rollback();
			}catch(Exception ex){
				ex.printStackTrace();
				throw new ProjectSchedulerException("Could not rollback transaction",ex);
			}
			e.printStackTrace();
			throw new ProjectSchedulerException(e.getMessage(),e);
		}

	}

	@Override
	public Activity findActivity(Activity activity) throws ProjectSchedulerException {
		UserTransaction userTransaction = null;
		Activity retrievedActivity = null;
		
		try {
			userTransaction = ServiceLocator.getInstance().lookupUserTransaction();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
			throw new ProjectSchedulerException("Could not create transaction object",e);
		}
		
		try {
			userTransaction.begin();
			DAOFactory daoFactory = DAOFactory.getDAOFactory();
			ActivityDAO activityDAO = daoFactory.getActivityDAO();
			retrievedActivity = activityDAO.find(activity);
			userTransaction.commit();
		} catch (Exception e) {
			try{
				userTransaction.rollback();
			}catch(Exception ex){
				ex.printStackTrace();
				throw new ProjectSchedulerException("Could not rollback transaction",ex);
			}
			e.printStackTrace();
			throw new ProjectSchedulerException(e.getMessage(),e);
		}
		return retrievedActivity;
	}

	@Override
	public ActivityListHandler listActivities() throws ProjectSchedulerException {
		UserTransaction userTransaction = null;
		ActivityListHandler listHandler = new ActivityListHandler();
		
		try {
			userTransaction = ServiceLocator.getInstance().lookupUserTransaction();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
			throw new ProjectSchedulerException("Could not create transaction object",e);
		}
		
		try {
			userTransaction.begin();
			DAOFactory daoFactory = DAOFactory.getDAOFactory();
			ActivityDAO activityDAO = daoFactory.getActivityDAO();
			listHandler.setList(activityDAO.list());
			userTransaction.commit();
		} catch (Exception e) {
			try{
				userTransaction.rollback();
			}catch(Exception ex){
				ex.printStackTrace();
				throw new ProjectSchedulerException("Could not rollback transaction",ex);
			}
			e.printStackTrace();
			throw new ProjectSchedulerException(e.getMessage(),e);
		}
		return listHandler;
	}

	@Override
	public ActivityListHandler listActivitiesByProject(Project project) throws ProjectSchedulerException {
		UserTransaction userTransaction = null;
		ActivityListHandler listHandler = new ActivityListHandler();
		
		try {
			userTransaction = ServiceLocator.getInstance().lookupUserTransaction();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
			throw new ProjectSchedulerException("Could not create transaction object",e);
		}
		
		try {
			userTransaction.begin();
			DAOFactory daoFactory = DAOFactory.getDAOFactory();
			ActivityDAO activityDAO = daoFactory.getActivityDAO();
			listHandler.setList(activityDAO.listByProject(project));
			userTransaction.commit();
		} catch (Exception e) {
			try{
				userTransaction.rollback();
			}catch(Exception ex){
				ex.printStackTrace();
				throw new ProjectSchedulerException("Could not rollback transaction",ex);
			}
			e.printStackTrace();
			throw new ProjectSchedulerException(e.getMessage(),e);
		}
		return listHandler;
	}
	
	@Override
	public void updateActivity(Activity activity) throws ProjectSchedulerException {
		UserTransaction userTransaction = null;
		
		try {
			userTransaction = ServiceLocator.getInstance().lookupUserTransaction();
		} catch (ServiceLocatorException e) {
			e.printStackTrace();
			throw new ProjectSchedulerException("Could not create transaction object",e);
		}
		
		try {
			userTransaction.begin();
			DAOFactory daoFactory = DAOFactory.getDAOFactory();
			ActivityDAO activityDAO = daoFactory.getActivityDAO();
			activityDAO.update(activity);
			userTransaction.commit();
		} catch (Exception e) {
			try{
				userTransaction.rollback();
			}catch(Exception ex){
				ex.printStackTrace();
				throw new ProjectSchedulerException("Could not rollback transaction",ex);
			}
			e.printStackTrace();
			throw new ProjectSchedulerException(e.getMessage(),e);
		}

	}

}
